#include "Linked Stack.h"

void STackInit(Stack* p)
{
	assert(p);
	p->top = NULL;
	p->size = 0;
}

void STackPush(Stack* p, STDataType x)
{
	assert(p);
	Node* NewNode = (Node*)malloc(sizeof(Node));
	if (NewNode == NULL)
	{
		perror("malloc error");
	}
	if (p->top == NULL)
	{
		NewNode->data = x;
		NewNode->next = NULL;
		p->top = NewNode;
	}
	else
	{
		NewNode->data = x;
		NewNode->next = p->top;
		p->top = NewNode;
	}
	p->size++;
}

void STackPop(Stack* p)
{
	assert(p && p->top);
	Node* temp = p->top;
	p->top = p->top->next;
	free(temp);
	p->size--;
}

void STackEmpty(Stack* p)
{
	assert(p);
	if (p->size == 0)
		return 0;
	else
		return 1;
}

STDataType STackTop(Stack* p)
{
	assert(p && p->top);
	return p->top->data;
}

void STackDestroy(Stack* p)
{
	assert(p);
	while (p->top != NULL)
	{
		Node* temp = p->top;
		p->top = p->top->next;
		free(temp);
		p->size--;
	}
}


